package com.zhq.blog.core.domain.game;

import java.util.Collection;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import com.zhq.blog.core.domain.BaseRepository;

/**
 * @title:GamePeriodRepository.java
 * @package com.zhq.blog.core.domain.game
 * @author 张恒强
 * @date 2014年12月27日 上午11:42:27
 * @version V1.0
 */
public interface GamePeriodRepository extends JpaRepository<GamePeriod,Long>,
			BaseRepository<GamePeriod>,JpaSpecificationExecutor<GamePeriod>{
	/**
	 * 查找未开奖的期数(包括准备开奖的期数) 固定获取为4
	 * @param date
	 * @param id
	 * @return
	 */
	@Query("select g from GamePeriod g where g.deleted = 0 and g.gameId = :id and g.state = 0 order by g.id desc")
	List<GamePeriod> find_not_lottery(@Param("id") long id);
	/**
	 * 查找系统启动之前 未开奖的期数  返回投注 并设置为删除
	 * @return
	 */
	@Query("select g from GamePeriod g where g.deleted = 0 and g.state = 0")
	List<GamePeriod> find_all_not_lottery();
	/**
	 * 查找要开奖的那一期  能够直接确定
	 * @param gameId
	 * @param period
	 * @return
	 */
	@Query("select g from GamePeriod g where g.deleted = 0 and g.gameId = :gameId and g.period = :period")
	List<GamePeriod> find_by_gameId_and_period(@Param("gameId") long gameId,@Param("period") long period);
	
	@Query("select max(g.period) from GamePeriod g where g.gameId = :gameId")
	Object find_by_gameId(@Param("gameId")long gameId);
	
	/**
	 * 查找某游戏的所有期数  
	 * @param gameId
	 * @return
	 */
	@Query("select g from GamePeriod g where g.gameId = :gameId order by g.id desc")
	Page<GamePeriod> find_all_by_gameId(@Param("gameId")long gameId,Pageable pageable);
	
	/**
	 * 查找某游戏的所有已开奖的期数  
	 * @param gameId
	 * @return
	 */
	@Query("select g from GamePeriod g where g.state =2 and g.gameId = :gameId order by g.id desc")
	Page<GamePeriod> find_all_lotteryed_by_gameId(@Param("gameId")long gameId,Pageable pageable);
	
	/**
	 * 查找要开奖的那一期   需根据条件判断
	 * 前台需要
	 */
	@Query("select g from GamePeriod g where g.gameId = :gameId and g.deleted = 0 and g.state = 0 order by g.period asc")
	List<GamePeriod> find_to_be_lottery_by_gameId_state(@Param("gameId") long gameId);
	
	/**
	 * 更新某游戏某期奖池总数
	 * @param gameId
	 * @param period
	 * @param money
	 */
	@Transactional
	@Modifying
	@Query("update GamePeriod gp set gp.totalMoney = gp.totalMoney + :money where gp.gameId = :gameId and gp.period = :period")
	void update_total_money(@Param("gameId") long gameId,@Param("period") long period,@Param("money")long money);
	
	/**
	 * 更新某游戏某期投注人数
	 * @param gameId
	 * @param period
	 * @param num
	 */
	@Transactional
	@Modifying
	@Query("update GamePeriod gp set gp.bettingUserNumber = gp.bettingUserNumber + :num where gp.gameId = :gameId and gp.period = :period")
	void update_bet_user_numbers(@Param("gameId") long gameId,@Param("period") long period,@Param("num")int num);
	
	
	@Query("select gp from GamePeriod gp where gp.period in :ids and gp.gameId = :gameId order by gp.period desc")
	Page<GamePeriod> find_by_game_periods(@Param("ids")Collection<Long> ids,@Param("gameId") long gameId,Pageable pageable);
}
